import { UeESVisualObject, UeViewer, ESTreeParam, ESSeparateFoliage, getFlyToCenterAndRotation, defaultFlyToRotation } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
import { ESForest, ESJTree } from ".";
import { addTreesCallFunc, removeAllTreesCallFunc, updateTreeParamsCallFunc, cutDownTreesCallFunc, growthSimulationCallFunc } from "./CallFunc";

export class UeESForest<T extends ESForest = ESForest> extends UeESVisualObject<T> {
    static readonly type = this.register<ESForest>(ESForest.type, this);

    static override combinationClass = true;

    private _eSSeparateFoliage = this.dv(new ESSeparateFoliage());
    get eSSeparateFoliage() { return this._eSSeparateFoliage; }

    constructor(sceneObject: T, ueViewer: UeViewer) {
        super(sceneObject, ueViewer);

        const viewer = ueViewer.viewer;
        if (!viewer) {
            console.warn(`UeESForest: viewer is undefined!`);
            return;
        }
        const eSSeparateFoliage = this.eSSeparateFoliage;
        ueViewer.add(eSSeparateFoliage);
        this.dispose(() => ueViewer.delete(eSSeparateFoliage))
        {
            const update = () => {
                eSSeparateFoliage.treeTypes = sceneObject.getTreeTypes("UE");
            }
            update();
            this.ad(sceneObject.treeTypesChanged.don(update))
        }
        {
            const update = (TreeParams: ESTreeParam[]) => {
                console.log("addTrees", TreeParams);
                eSSeparateFoliage.addTrees(TreeParams)

                // eSSeparateFoliage.addTrees(TreeParams)
                // @ts-ignore
                // addTreesCallFunc(ueViewer, sceneObject.id, TreeParams)
            }
            if (sceneObject.trees.length != 0) {
                const switchDate = sceneObject.formatData(sceneObject.trees, sceneObject.getTreeTypes());
                update(switchDate);
            }
            this.dispose(sceneObject.addEvent.disposableOn(update));
        }

        // this.dispose(sceneObject.removeAllEvent.disposableOn(() => {
        //     eSSeparateFoliage.removeAllTrees()
        //     // removeAllTreesCallFunc(ueViewer, sceneObject.id)
        // }));

        // this.dispose(sceneObject.updateEvent.disposableOn((TreeParams: ESTreeParam[]) => {
        //     eSSeparateFoliage.updateTreeParams(TreeParams)
        //     // @ts-ignore
        //     // updateTreeParamsCallFunc(ueViewer, sceneObject.id, TreeParams)
        // }));

        // this.dispose(sceneObject.cutDownEvent.disposableOn((TreeIds, TimeLength) => {
        //     // @ts-ignore
        //     cutDownTreesCallFunc(ueViewer, sceneObject.id, TreeIds, TimeLength)
        // }));

        // this.dispose(sceneObject.growthEvent.disposableOn((ToParams, TimeLength, SwitchTime) => {
        //     // 格式转换

        //     // @ts-ignore
        //     growthSimulationCallFunc(ueViewer, sceneObject.id, ToParams, TimeLength, SwitchTime)
        // }));

        this.ad(sceneObject.flyToTreeEvent.don((treeId: string,) => {

            const tree = sceneObject.trees.find(tree => tree.id === treeId) as ESJTree;
            const position = tree.position;
            const rotation = [...defaultFlyToRotation] as [number, number, number];
            // const { position: flyPosition, rotation: flyRotation } = getFlyToCenterAndRotation(position, rotation, 100);
            ueViewer.flyTo({
                "distance": 1.0669640486491471e-9,
                "heading": 212.408489024946,
                "pitch": 23.0931545496113,
                "flyDuration": 1,
                "hDelta": -206.2479135934638,
                "pDelta": -82.84701664437438
            }, position)
        }))
        this.ad(sceneObject.flyToEvent.don((duration, id) => {
            eSSeparateFoliage.flyTo(duration);
        }))
    }
}